﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsolePro.Practice
{
    /// <summary>
    /// 自旋锁练习
    /// </summary>
    public class SpinLockPratice
    {
        public void Test()
        {
            int count = 0;
            var taskArray = new Task[1000];
            var sl = new SpinLock();
            for (int i = 0; i < 1000; i++)
            {
                taskArray[i] = Task.Run(() => {
                    bool gotLock = false;     //释放成功
                    sl.Enter(ref gotLock);    //进入锁
                    count++;
                    if (gotLock) sl.Exit();  //释放
                });
            }
            Task.WaitAll(taskArray);
            Console.WriteLine(count);
        }

    }
}
